package psn.hui.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.domain.Menu;
import psn.hui.sys.domain.User;
import psn.hui.sys.service.MenuService;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;
import psn.hui.sys.utils.MenuTreeBuilder;
import psn.hui.sys.utils.TreeNode;
import psn.hui.sys.utils.WebUtils;
import psn.hui.sys.vo.MenuVo;

/**
 * 菜单管理控制器
 * 
 * @author hui
 *
 */
@RestController
@RequestMapping("menu")
public class MenuController {

	@Autowired
	private MenuService menuService;
	/**
	 * 	获取主页左侧菜单树
	 * @param menuVo
	 * @return
	 */
	@RequestMapping("loadIndexLeftMenuJson")
	public List<TreeNode> loadIndexLeftMenuJson(MenuVo menuVo) {
		// 得到当前登录用户对象
		User user = (User) WebUtils.getHttpSession().getAttribute("user");
		List<Menu> menulist = null;
		menuVo.setAvailable(SysConstant.MENU_AVAILABLE_TRUE);
		if (user.getType() == SysConstant.USER_TYPE_SUPER) {
			menulist = menuService.queryAllMenuForList(menuVo);
		} else {
			menulist = menuService.queryMenuByUserIdForList(menuVo, user.getUserid());
		}
		// 把menulist中的数据转换为树结点放到nodes中
		Boolean spread = false;
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for (Menu menu : menulist) {
			spread = menu.getSpread() == SysConstant.MENU_SPREAD_TRUE;
			nodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), menu.getIcon(), menu.getHref(), spread,
					menu.getTarget()));
		}
		return MenuTreeBuilder.buildMenuTree(nodes, SysConstant.MENU_ROOTMENU_ID);
	}
	
	/**
	 * 获取菜单管理页左侧菜单树
	 */
	@RequestMapping("loadMenuTreeLeft")
	public DataGridView loadMenuTreeLeft() {
		List<Menu> menulist = null;
		MenuVo menuVo = new MenuVo();
		menulist = menuService.queryAllMenuForList(menuVo);
		Boolean spread = false;
		List<TreeNode> nodes = new ArrayList<TreeNode>();
		for (Menu menu : menulist) {
			spread = menu.getSpread() == SysConstant.MENU_SPREAD_TRUE;
			nodes.add(new TreeNode(menu.getId(), menu.getPid(), menu.getTitle(), menu.getIcon(), menu.getHref(), spread,
					menu.getTarget()));
		}
		return new DataGridView(nodes);
	}
	
	/**
	 * 获取菜单列表，分页，查询
	 */
	@RequestMapping("menuList")
	public DataGridView menuList(MenuVo menuVo) {
		System.out.println("id:"+menuVo.getId()+",title:"+menuVo.getTitle());
		List<Menu> menulist = null;
		Page<Menu> page = PageHelper.startPage(menuVo.getPage(), menuVo.getLimit());
		menulist = menuService.queryAllMenuForList(menuVo);
		return  new DataGridView(page.getTotal(), menulist);
	}
	/**
	 * 添加菜单
	 * @param menu
	 * @return
	 */
	@RequestMapping("addMenu")
	public JsonResult addMenu(Menu menu) {
		try {
			int rs = menuService.insertSelective(menu);
			if(rs > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}
	/**
	 * 修改菜单
	 * @param menu
	 * @return
	 */
	@RequestMapping("updateMenu")
	public JsonResult updateMenu(Menu menu) {
		try {
			int rs = menuService.updateByPrimaryKeySelective(menu);
			if(rs > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}
	/**
	 * 判断菜单是否拥有子菜单节点
	 */
	@RequestMapping("hasChildren")
	public JsonResult hasChildren(Integer id) {
		int count = menuService.countChildrenById(id);
		if(count > 0) {
			return SysConstant.HAS_CHILDREN_TRUE;
		}
		return SysConstant.HAS_CHILDREN_FALSE;
		
	}
	/**
	 * 根据id删除菜单
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteById")
	public JsonResult deleteById(Integer id) {
		try {
			int rs = menuService.deleteById(id);
			if(rs > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
	
}
